package com.fx.spring.jfx;

import com.fx.spring.context.ApplicationContextFX;
import com.fx.spring.util.SpringContextUtil;
import javafx.application.Application;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ApplicationStageFX extends Application {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final ApplicationContextFX contextFX = SpringContextUtil.getBean(ApplicationContextFX.class);
    private final AbstractSpringStartFX startClass = contextFX.getStartClass();

    // todo  异常使用统一处理可以自定提醒
    @Override
    public void init() throws Exception {
        try {
            logger.debug("JavaFX 初始化中。。。");
            startClass.init();
            super.init();
        } catch (Exception e) {
            logger.error("JavaFX 初始化失败", e);
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            contextFX.setPrimaryStage(primaryStage);
            startClass.start(primaryStage);
            contextFX.startUpTime(System.currentTimeMillis());
            logger.info("JavaFX 启动完成,启动耗时[{}(ms)]", contextFX.getStartUpTime());
        } catch (Exception e) {
            logger.error("JavaFX 运行失败", e);
        }
    }

    @Override
    public void stop() throws Exception {
        try {
            super.stop();
            startClass.stop();
            SpringContextUtil.close();
            logger.debug("关闭程序。。。");
        } catch (Exception e) {
            logger.error("JavaFX 关闭失败", e);
        }
    }
}
